package com.caigou.modules.center.ui.detail

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.center.R
import com.caigou.modules.center.adapter.FlowerAdapter
import com.caigou.modules.center.bean.FlowerBean
import com.caigou.modules.center.databinding.FragmentFlowerBinding
import com.caigou.modules.center.viewmodel.DetailViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/11/03
 */
@AndroidEntryPoint
class FlowerFragment : BaseFragment() {

    private var _binding: FragmentFlowerBinding? = null
    private val binding get() = _binding!!

    private val detailViewModel: DetailViewModel by viewModels()

    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(requireActivity())
    }

    private val mAdapter: FlowerAdapter by lazy {
        FlowerAdapter(mutableListOf())
    }

    private val recyclerViewItemDecoration by lazy {
        SpaceItemDecoration(requireActivity())
    }

    private var page: Long = 1L
    private val limit: Long = 10L
    private var totalPage = 0

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentFlowerBinding.inflate(inflater, container, false)
        return _binding?.root
    }


    override fun initView() {
        binding.flowerSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                mAdapter.loadMoreModule.isEnableLoadMore = false
                page = 1L
                detailViewModel.getFlowerList(page, limit, AppBackgroundInfo.instance.userBean!!.userId)
            }
        }

        mAdapter.loadMoreModule.run {
            setOnLoadMoreListener {
                mAdapter.loadMoreModule.isEnableLoadMore = true
                detailViewModel.getFlowerList(page, limit, AppBackgroundInfo.instance.userBean!!.userId)
            }
            isAutoLoadMore = true
            isEnableLoadMoreIfNotFullPage = false
        }
        binding.flowerRecyclerViewList.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(recyclerViewItemDecoration)
        }

        binding.flowerTvBack.setOnClickListener {
            (requireActivity() as DetailActivity).onBackPressed()
        }
    }

    override fun lazyLoad() {
        detailViewModel.getFlowerList(page, limit, AppBackgroundInfo.instance.userBean!!.userId)
    }

    override fun onBack() {
    }

    override fun observeViewModel() {
        observe(detailViewModel.flowerLiveData, ::handleFlowerList)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun handleFlowerList(result: Resource<FlowerBean>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.flowerSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取关注信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.flowerSwipeRefreshLayout.isRefreshing = false
                mAdapter.loadMoreModule.isEnableLoadMore = true

                if (result.data == null) {
                    return
                }
                binding.flowerTvNum.text = "当前小红花数量：${result.data!!.totalCount}朵"
                totalPage = result.data!!.totalPage

                if (page == 1L) {
                    mAdapter.setList(result.data!!.list)
                } else {
                    mAdapter.addData(result.data!!.list)
                }

                if (result.data!!.pageSize < limit) {
                    mAdapter.loadMoreModule.loadMoreEnd(false)
                } else {
                    mAdapter.loadMoreModule.loadMoreComplete()
                }

                if (page >= totalPage) {
                    mAdapter.loadMoreModule.loadMoreEnd(false)
                } else {
                    page++
                }
            }
        }
    }
}